package admin

import (
	"chaowan/data_source"
	"chaowan/models"
	"github.com/gin-gonic/gin"
	"net/http"
)

type OrderlistRBody struct {
	Data       []models.Ordergoodsql `json:"data"`
	PageNo     int                   `json:"pageNo"`
	PageSize   int                   `json:"pageSize"`
	TotalCount int64                 `json:"totalCount"`
	TotalPage  int64                 `json:"totalPage"`
}

type OrderlistBody struct {
	Page     int    `json:"page"`
	Pagesize int    `json:"pagesize"`
	Laiyuan  int64  `json:"laiyuan"`
	Name     string `json:"name"`
	Phone    string `json:"phone"`
}

func Orderlist(ctx *gin.Context) {
	request := OrderlistBody{}
	ctx.ShouldBind(&request)
	laiyuan := request.Laiyuan
	name := request.Name
	phone := request.Phone
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Ordergoodsql
	res := data_source.Db.Model(&models.Ordergood{}).Select("ordergood.*, goods.name as goodname, goods.image as goodimage, user.phone, user.nickname, user.laiyuan").Joins("left join goods on goods.id = ordergood.goodid").Joins("left join user on user.id = ordergood.uid").Where("ordergood.status = 2")
	if laiyuan > 0 {
		res = res.Where("user.laiyuan = ?", laiyuan)
	}
	if phone != "" {
		res = res.Where("user.phone = ?", phone)
	}
	if name != "" {
		res = res.Where("user.nickname like ? or goods.name like ?", "%"+name+"%", "%"+name+"%")
	}
	res.Model(&models.Ordergood{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := OrderlistRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}

type OrderfslistRBody struct {
	Data       []models.Orderfanshangsql `json:"data"`
	PageNo     int                       `json:"pageNo"`
	PageSize   int                       `json:"pageSize"`
	TotalCount int64                     `json:"totalCount"`
	TotalPage  int64                     `json:"totalPage"`
}

func Orderfslist(ctx *gin.Context) {
	request := OrderlistBody{}
	ctx.ShouldBind(&request)
	laiyuan := request.Laiyuan
	name := request.Name
	phone := request.Phone
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Orderfanshangsql
	res := data_source.Db.Model(&models.Orderfanshang{}).Select("orderfanshang.*, fanshang.name, fanshang.image, user.phone, user.nickname, user.laiyuan").Joins("left join fanshang on fanshang.id = orderfanshang.fanshangid").Joins("left join user on user.id = orderfanshang.uid").Where("orderfanshang.status = 2")

	if laiyuan > 0 {
		res = res.Where("user.laiyuan = ?", laiyuan)
	}
	if phone != "" {
		res = res.Where("user.phone = ?", phone)
	}
	if name != "" {
		res = res.Where("user.nickname like ? or fanshang.name like ?", "%"+name+"%", "%"+name+"%")
	}

	res.Model(&models.Orderfanshang{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := OrderfslistRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}

type OrderndlistRBody struct {
	Data       []models.Orderniudansql `json:"data"`
	PageNo     int                     `json:"pageNo"`
	PageSize   int                     `json:"pageSize"`
	TotalCount int64                   `json:"totalCount"`
	TotalPage  int64                   `json:"totalPage"`
}

func Orderndlist(ctx *gin.Context) {
	request := OrderlistBody{}
	ctx.ShouldBind(&request)
	laiyuan := request.Laiyuan
	name := request.Name
	phone := request.Phone
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Orderniudansql
	res := data_source.Db.Model(&models.Orderniudan{}).Select("orderniudan.*, niudan.name, niudan.image, user.phone, user.nickname, user.laiyuan").Joins("left join niudan on niudan.id = orderniudan.niudanid").Joins("left join user on user.id = orderniudan.uid").Where("orderniudan.status = 2")
	if laiyuan > 0 {
		res = res.Where("user.laiyuan = ?", laiyuan)
	}
	if phone != "" {
		res = res.Where("user.phone = ?", phone)
	}
	if name != "" {
		res = res.Where("user.nickname like ? or niudan.name like ?", "%"+name+"%", "%"+name+"%")
	}
	res.Model(&models.Orderniudan{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := OrderndlistRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}
